package com.ruoyi.web.controller.creditlife;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.creditlife.domain.ComplianceRecord;
import com.ruoyi.creditlife.service.IComplianceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户守约记录Controller
 * 
 * @author ddd
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/creditlife/record")
public class ComplianceRecordController extends BaseController
{
    @Autowired
    private IComplianceRecordService complianceRecordService;

    /**
     * 获取用户守约统计数据
     */
    @GetMapping("/stats")
    public AjaxResult stats()
    {
        Long userId = SecurityUtils.getUserId();
//        log.info("获取用户守约统计数据，用户ID: {}", userId);

        // 获取总守约次数
        int totalRecords = complianceRecordService.countComplianceRecords(userId);

        // 获取近期守约率（假设近3个月）
        double recentComplianceRate = complianceRecordService.getRecentComplianceRate(userId, 3);

        // 获取守约类型分布
        List<Map<String, Object>> typeDistribution = complianceRecordService.getTypeDistribution(userId);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalRecords", totalRecords);
        resultMap.put("recentComplianceRate", recentComplianceRate);
        resultMap.put("typeDistribution", typeDistribution);
        return AjaxResult.success(resultMap);
    }
    /**
     * 查询用户守约记录列表
     */
    @PreAuthorize("@ss.hasPermi('creditlife:record')")
    @GetMapping("/list")
    public TableDataInfo list(ComplianceRecord complianceRecord)
    {
        startPage();
        complianceRecord.setUserId(SecurityUtils.getUserId());
        List<ComplianceRecord> list = complianceRecordService.selectComplianceRecordList(complianceRecord);
        return getDataTable(list);
    }

    /**
     * 导出用户守约记录列表
     */
    @PreAuthorize("@ss.hasPermi('creditlife:record')")
    @Log(title = "用户守约记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComplianceRecord complianceRecord)
    {
        List<ComplianceRecord> list = complianceRecordService.selectComplianceRecordList(complianceRecord);
        ExcelUtil<ComplianceRecord> util = new ExcelUtil<ComplianceRecord>(ComplianceRecord.class);
        util.exportExcel(response, list, "用户守约记录数据");
    }

    /**
     * 获取用户守约记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('creditlife:record')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(complianceRecordService.selectComplianceRecordById(id));
    }

    /**
     * 新增用户守约记录
     */
    @PreAuthorize("@ss.hasPermi('creditlife:record')")
    @Log(title = "用户守约记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComplianceRecord complianceRecord)
    {
        return toAjax(complianceRecordService.insertComplianceRecord(complianceRecord));
    }

    /**
     * 修改用户守约记录
     */
    @PreAuthorize("@ss.hasPermi('creditlife:record')")
    @Log(title = "用户守约记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComplianceRecord complianceRecord)
    {
        return toAjax(complianceRecordService.updateComplianceRecord(complianceRecord));
    }

    /**
     * 删除用户守约记录
     */
    @PreAuthorize("@ss.hasPermi('creditlife:record')")
    @Log(title = "用户守约记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(complianceRecordService.deleteComplianceRecordByIds(ids));
    }
}
